﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace ColorWanted.Plugin.ExcelToJSON
{
    public partial class PluginForm : Form
    {
        private OpenFileDialog openFileDialog;
        private SaveFileDialog saveFileDialog;
        private FolderBrowserDialog folderBrowserDialog;

        public PluginForm()
        {
            InitializeComponent();

            openFileDialog = new OpenFileDialog();
            saveFileDialog = new SaveFileDialog();
            folderBrowserDialog = new FolderBrowserDialog();
        }

        private void btnFileExcel_Click(object sender, EventArgs e)
        {
            openFileDialog.Reset();
            openFileDialog.Filter = "Excel|*.xls|Excel|*.xlsx";
            openFileDialog.CheckFileExists = true;

            var current = tbFileExcel.Text;
            if (!string.IsNullOrWhiteSpace(current) && File.Exists(current))
            {
                openFileDialog.FileName = current;
            }

            var result = openFileDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                tbFileExcel.Text = openFileDialog.FileName;
            }
        }

        private void btnFileJson_Click(object sender, EventArgs e)
        {

            saveFileDialog.Reset();
            saveFileDialog.Filter = "JSON|*.json";
            saveFileDialog.InitialDirectory = Path.GetDirectoryName(tbFileExcel.Text);
            var current = tbFileJson.Text;
            if (!string.IsNullOrWhiteSpace(current) && File.Exists(current))
            {
                saveFileDialog.FileName = current;
            }

            var result = saveFileDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                tbFileJson.Text = saveFileDialog.FileName;
            }
        }

        private void btnFileOk_Click(object sender, EventArgs e)
        {
            var inputPath = tbFileExcel.Text.Trim();
            var outputPath = tbFileJson.Text.Trim();

            if (string.IsNullOrWhiteSpace(inputPath))
            {
                MessageBox.Show(this, "请指定要处理的 Excel 文件");
                return;
            }
            if (string.IsNullOrWhiteSpace(outputPath))
            {
                MessageBox.Show(this, "请指定输出的 JSON 文件");
                return;
            }

            try
            {
                ProcessFile(inputPath, outputPath);
                MessageBox.Show(this, "处理完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "处理失败:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDirExcel_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Reset();

            var current = tbDirExcel.Text;
            if (!string.IsNullOrWhiteSpace(current) && Directory.Exists(current))
            {
                folderBrowserDialog.SelectedPath = current;
            }

            var result = folderBrowserDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                tbDirExcel.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnDirJson_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Reset();

            var current = tbDirJson.Text;
            if (!string.IsNullOrWhiteSpace(current) && Directory.Exists(current))
            {
                folderBrowserDialog.SelectedPath = current;
            }

            var result = folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                tbDirJson.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnDirOk_Click(object sender, EventArgs e)
        {
            var inputPath = tbDirExcel.Text.Trim();
            var outputPath = tbDirJson.Text.Trim();

            if (string.IsNullOrWhiteSpace(inputPath))
            {
                MessageBox.Show(this, "请指定要处理的 Excel 目录");
                return;
            }
            if (string.IsNullOrWhiteSpace(outputPath))
            {
                MessageBox.Show(this, "请指定输出的 JSON 目录");
                return;
            }

            string filename = "";
            try
            {
                var files = Directory.GetFiles(inputPath, "*.xls;*.xlsx");
                foreach (var file in files)
                {
                    filename = Path.GetFileNameWithoutExtension(file);
                    var outputFile = Path.Combine(outputPath, filename + ".json");

                    ProcessFile(inputPath, outputPath);
                }
                MessageBox.Show(this, $"处理完成，共处理 {files.Length} 个文件");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, $"处理文件 {filename} 失败:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ProcessFile(string excelFile, string jsonFile)
        {
            var data = ExcelLoader.Load(excelFile);
            var serializer = new JavaScriptSerializer();
            StringBuilder sb = new StringBuilder();
            serializer.Serialize(data, sb);
            File.WriteAllText(jsonFile, sb.ToString());
        }
    }
}
